package com.IClass;


import java.io.IOException;

import android.content.Context;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
	private SurfaceHolder mHolder;  
    private Camera mCamera; 
	
	public CameraPreview(Context context, Camera camera) {
		super(context);
		mCamera = camera;  
        mHolder = this.getHolder();  
        mHolder.addCallback(this);  
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  
        mHolder.setFormat(PixelFormat.TRANSPARENT);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		 if (mHolder.getSurface() == null)  {
			 System.out.println("mHolder.getSurface() == null");
			  return;  
		 }
	        try {  
	        	mCamera.stopPreview(); 
	            mCamera.setPreviewDisplay(mHolder);  
	            mCamera.startPreview();
	        } catch (IOException e) {  
	        	Log.d("surfaceChanged", "Error starting camera preview: " + e.getMessage());
	        }  
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
	        try {  
	            mCamera.setPreviewDisplay(holder);  
	            mCamera.startPreview();  
	        } catch (IOException e) {  
	        	Log.d("surfaceCreated", "Error setting camera preview: " + e.getMessage());
	        }  
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
	}
}


